package com.tp.ssm.controller.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class CustomExceptionResolver implements HandlerExceptionResolver{

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		//handler表示传过来的Handler对象
		//ex表示全局捕获异常捕获到的异常
		ex.printStackTrace(); 
		
		CustomException exception = null ;
		if(ex instanceof CustomException){
			exception = (CustomException) ex ;
		}else{
			exception = new CustomException("未知错误，请与管理员联系") ;
		}
		String message = exception.getMessage() ;
		ModelAndView modelAndView = new ModelAndView() ;
		modelAndView.addObject("message", message) ;
		modelAndView.setViewName("error");
		
		return modelAndView;
	}
	

}
